package com.weizhu.swufer.core.client;

import com.weizhu.swufer.core.cluster.Cluster;
import com.weizhu.swufer.core.entity.InvokeParam;
import com.weizhu.swufer.core.rpc.RpcRequest;

import java.util.concurrent.Future;

public class DefaultGenericService implements GenericService{

    private ReferenceConfig referenceConfig;

    private Invoker invoker;

    public DefaultGenericService(ReferenceConfig referenceConfig){
        this.referenceConfig = referenceConfig;
        invoker = Cluster.getClusterInvoker(referenceConfig);
    }

    @Override
    public Object $invoke(String service, String method, Object[] params) throws Exception {
        InvokeParam invokeParam = new InvokeParam();
        RpcRequest rpcRequest = new RpcRequest();
        rpcRequest.setInterfaceName(service);
        rpcRequest.setMethodName(method);
        rpcRequest.setArgs(params);
        rpcRequest.setMaxWaitTime(referenceConfig.getMaxWaitTime());
        invokeParam.setRpcRequest(rpcRequest);
        invokeParam.setTargetAddress(referenceConfig.getTargetAddress());
        invokeParam.setTryTimes(referenceConfig.getTryTimes());
        return invoker.invoke(invokeParam);

    }

    @Override
    public Future $invokeAsync(String service, String method,Object... params) {


        return null;
    }

}
